package com.coolw.data.migrate.common.task;

import com.coolw.data.migrate.enums.MigrateTypeEnum;
import com.coolw.data.migrate.domain.request.DataMigrateRequest;
import com.coolw.data.migrate.domain.response.DataMigrateResponse;
import com.coolw.data.migrate.process.strategy.DataMigrateContext;
import com.coolw.data.migrate.process.strategy.DataMigrateProcess;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;

/**
 * 数据迁移任务
 *
 * @author coolw
 * @date 2024/2/20 9:11
 */
@Slf4j
@Component
public class DataMigrateTask {

    @Resource
    private DataMigrateContext dataMigrateContext;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void testDataMigrate() {
        log.info("数据迁移-任务执行开始");

        StopWatch stopWatch = new StopWatch();
        stopWatch.start("数据迁移");

        DataMigrateRequest request = new DataMigrateRequest();
        request.setSourceTableName("hub_channel_request");
        request.setTargetTableName("hub_channel_request_240201");

        // 多少天之前
        /*request.setMigrateType(MigrateTypeEnum.BEFORE_DAY.getCode());
        request.setBeforeDay(30);*/

        // 固定大小
        request.setMigrateType(MigrateTypeEnum.FIXED_SIZE.getCode());
        request.setFixedSize(5000);

        // 指定日期
        /*request.setMigrateType(MigrateTypeEnum.SPECIFIED_DATE.getCode());
        request.setSpecifiedDate("2024-01-01");*/

        // 时间范围
        /*request.setMigrateType(MigrateTypeEnum.BETWEEN_DATE.getCode());
        request.setStartDate("2024-01-01");
        request.setEndDate("2024-01-08");*/

        DataMigrateProcess dataMigrateProcess = dataMigrateContext.getProcess(request.getMigrateType());
        DataMigrateResponse response = dataMigrateProcess.process(request);

        stopWatch.stop();

        log.info("数据迁移-任务执行结束,执行结果:{},耗时 {} ms", response, stopWatch.getTotalTimeMillis());
    }
}